package com.easy.activiti.command;

import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.ExecutionEntityManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

/**
 * @author zhouchuang
 * 2024-11-07 14:23:14
 */
public class RejectCmd implements Command<Void> {
    protected Logger logger = LoggerFactory.getLogger(getClass());

    /** 流程实例id */
    private String processInstanceId;

    public RejectCmd(String processInstanceId) {
        this.processInstanceId = processInstanceId;
    }

    @Override
    public Void execute(CommandContext commandContext) {
        logger.info("node jump: processInstanceId={}", processInstanceId);
        ExecutionEntityManager executionEntityManager = commandContext.getExecutionEntityManager();
        ExecutionEntity executionEntity = executionEntityManager.findById(processInstanceId);
        Assert.isTrue(executionEntity != null, "processInstanceId is not exist");
        RuntimeService runtimeService = ProcessEngines.getDefaultProcessEngine().getRuntimeService();
        runtimeService.deleteProcessInstance(processInstanceId, "流程被拒绝");
        return null;
    }
}
